package com.hnqcgczy.dao.imp;

import com.hnqcgczy.dao.BaseDao;
import com.hnqcgczy.dao.IProductDao;
import com.hnqcgczy.db.MyTransaction;
import com.hnqcgczy.vo.TProduct;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.RowProcessor;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/***
 * 实现商品Dao
 */
public class ProductDaoImpl extends BaseDao implements IProductDao {
    MyTransaction myTransaction;
    //构造方法
    public ProductDaoImpl(MyTransaction myTransaction){
        //实现BaseDao初始化
        super(myTransaction);
        //因为dbutils需要连接对象
        this.myTransaction=myTransaction;
    }



    @Override
    public List<TProduct> getProductList(int count) throws SQLException {
        //limit 查询前多少条
        String sql="select * from t_product limit ?";
//        //将返回结果集映射为List集合
        List<TProduct> list=new ArrayList<TProduct>();
        //返回结果
        QueryRunner queryRunner=new QueryRunner();
        //设置将数据库下划线转成驼峰命名
        GenerousBeanProcessor beanProcessor=new GenerousBeanProcessor();
        //映射
        RowProcessor rowProcessor=new BasicRowProcessor(beanProcessor);
        //使用DbUtils工具完成查询
        list=queryRunner.query(myTransaction.getConn(),sql,new BeanListHandler<TProduct>(TProduct.class,rowProcessor),count);
        myTransaction.closeConn();
        return list;
    }

    @Override
    public TProduct getProductById(int tpid) {
        //预编译SQL语句
        String sql="select * from t_product where tp_id =?";
        //调用BaseDao查询返回结果集ResultSet
        ResultSet rs=this.query(sql,tpid);
        //遍历结果集
        TProduct product=null;
        try {
            while (rs.next()){
                //将查询到的一条记录转换成Product对象
                 product=tableToClass(rs);
            }

            return product;
        }catch (Exception e){
            e.printStackTrace();
            //发生异常
            return  null;
        }
    }

    @Override
    public List<TProduct> getProductList(String proName, int count) {
        //由于模糊商品名称查询是用户来决定，所以SQL语句需要动态拼接
        //使用StringBuffer动态拼接SQL
        StringBuffer sql=new StringBuffer("select * from t_product where 1=1");
        List<Object> params=new ArrayList<Object>();
        if(proName!=null&&!proName.equals("")){
            sql.append(" and tp_name like ?");
            params.add("%"+proName+"%");
        }
        sql.append(" limit ?");
        params.add(count);
        List<TProduct> list=new ArrayList<TProduct>();
        //返回结果
        QueryRunner queryRunner=new QueryRunner();
        //设置将数据库下划线转成驼峰命名
        GenerousBeanProcessor beanProcessor=new GenerousBeanProcessor();
        //映射
        RowProcessor rowProcessor=new BasicRowProcessor(beanProcessor);
        //使用DbUtils工具完成查询
        try {
            list=queryRunner.query(myTransaction.getConn(),sql.toString(),new BeanListHandler<TProduct>(TProduct.class,rowProcessor),params.toArray());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        myTransaction.closeConn();
        return list;
    }

    /***
     * 统一将t_product表里面的记录转行成product对象
     * 数据库字段 -- Java 属性
     * @param rs
     * @return
     */
    public TProduct tableToClass(ResultSet rs) throws SQLException {
        TProduct product=new TProduct();
        product.setTpId(rs.getInt("tp_id"));
        product.setTpName(rs.getString("tp_name"));
        product.setTpDescription(rs.getString("tp_description"));
        product.setTpStock(rs.getInt("tp_stock"));
        product.setTpcId(rs.getInt("tpc_id"));
        product.setTpcChildId(rs.getInt("tpc_child_id"));
        product.setTpFileName(rs.getString("tp_file_name"));
        product.setTpPrice(rs.getFloat("tp_price"));
        return product;
    }

    public static void main(String[] args) throws SQLException {
        //测试DbUtils查询
        MyTransaction myTransaction=new MyTransaction(true);
        IProductDao productDao=new ProductDaoImpl(myTransaction);
        List<TProduct> list=productDao.getProductList(10);
        //List<TProduct> list=productDao.getProductList("2",10);
        list.forEach(s-> System.out.println(s.toString()));
    }

}
